﻿/*******************************************************************
 * Module：XMLConfigUtil.cs
 * Author：LYH
 * Created：2011-04-08 14:46:28
 * Description：C#中配置文件的操作（增删改读）
 * *******************************************************************/
using System;
using System.Data;
using System.IO;
using System.Xml;
using System.Web;

namespace HJ212_Common
{


    /// 
    /// xml操作类
    /// 
    public class XMLConfigUtil
    {
        protected string strXmlFile;
        public XmlDocument objXmlDoc = new XmlDocument();

        public XMLConfigUtil(string XmlFile)
        {
            // 
            // TODO: 
            // 
            try
            {
                objXmlDoc.Load(XmlFile);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            strXmlFile = XmlFile;
        }

        public DataView GetData(string XmlPathNode)
        {
            //查找数据。返回一个DataView 
            DataSet ds = new DataSet();
            StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
            ds.ReadXml(read);
            return ds.Tables[0].DefaultView;
        }

        public void Replace(string XmlPathNode, string Content)
        {
            //更新节点内容。 
            objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
        }

        public void Delete(string Node)
        {
            //删除一个节点。 
            string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
            objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
        }

        public void InsertNode(string MainNode, string ChildNode, string Element, string Content)
        {
            //插入一节点和此节点的一子节点。 
            XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
            objRootNode.AppendChild(objChildNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.InnerText = Content;
            objChildNode.AppendChild(objElement);
        }

        public void InsertElement(string MainNode, string Element, string Attrib, string AttribContent, string Content)
        {
            //插入一个节点，带一属性。 
            XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.SetAttribute(Attrib, AttribContent);
            objElement.InnerText = Content;
            objNode.AppendChild(objElement);
        }

        public void InsertElement(string MainNode, string Element, string Content)
        {
            //插入一个节点，不带属性。 
            XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.InnerText = Content;
            objNode.AppendChild(objElement);
        }

        public void ModifyNodeValue(string XPath, string AttrName, string value)
        {
            XmlNode connXmlNd = objXmlDoc.SelectSingleNode(XPath);
            XmlElement xeConn = (XmlElement)connXmlNd;//将子节点类型转换为XmlElement类型
            xeConn.SetAttribute(AttrName, value);
            //Save();
        }
        public string GetNodeValue(string XPath, string AttrName)
        {
            XmlNode connXmlNd = objXmlDoc.SelectSingleNode(XPath);
            XmlElement xeConn = (XmlElement)connXmlNd;//将子节点类型转换为XmlElement类型
            return xeConn.GetAttribute(AttrName);
            //Save();
        }
        public void Save()
        {
            //保存文檔。 
            try
            {
                objXmlDoc.Save(strXmlFile);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            objXmlDoc = null;
        }
    }


}
